<template>
  <el-dialog
    title="数据转换样例列表"
    :close-on-click-modal="false"
    class="jsbos-dialog jsbos-dialog_center form-script-dialog"
    lock-scroll
    append-to-body
    v-bind="$attrs"
    width="800px"
    style
    :modal-append-to-body="false"
    v-on="$listeners"
  >
    <div class="jsbos-common-layout">
      <div class="jsbos-common-layout-center">
        <div class="jsbos-common-layout-main jsbos-flex-main">
          <div class="jsbos-common-head">
            <div style="display: block">
              <el-input v-model="query.name" placeholder="请输入" clearable>
                <el-button
                  slot="append"
                  type="primary"
                  icon="el-icon-search"
                  @click="search()"
                  >查询</el-button
                >
              </el-input>
            </div>
            <div class="jsbos-common-head-right">
              <el-button
                type="primary"
                icon="el-icon-plus"
                @click="addOrUpdateHandle()"
                >新增
              </el-button>
            </div>
          </div>
          <el-table
            v-loading="listLoading"
            :data="list"
            ref="tableRef"
            :border="true"
            :isHeader="true"
            highlight-current-row
            @current-change="handleCurrentChange"
            @row-dblclick="onClose"
          >
            <el-table-column prop="name" label="名称" width="0" align="left" />
            <el-table-column
              prop="sortCode"
              label="排序号"
              width="100"
              align="center"
            />
            <el-table-column label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                  >编辑
                </el-button>
                <el-button
                  type="text"
                  class="jsbos-table-delBtn"
                  @click="handleDel(scope.row.id)"
                  >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            :total="total"
            :page.sync="listQuery.currentPage"
            :limit.sync="listQuery.pageSize"
            @pagination="initData"
          />
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">{{
        $t("common.cancelButton")
      }}</el-button>
      <el-button type="primary" @click="onClose()">{{
        $t("common.confirmButton")
      }}</el-button>
    </span>
    <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
  </el-dialog>
</template>

<script>
import request from "@/utils/request";
import JSBOSForm from "./FormatForm";

export default {
  components: { JSBOSForm },
  data() {
    return {
      detailVisible: false,
      query: {
        name: undefined,
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      currentRow: null,
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        sort: "asc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "name", label: "名称" },
        { prop: "dataSample", label: "样例数据" },
      ],
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    handleCurrentChange(row) {
      this.currentRow = row;
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/system/BaseDataInterfaceFormat/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.$nextTick(() => {
          this.list = _list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        });
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/system/BaseDataInterfaceFormat/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail);
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "asc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
    onClose() {
      this.$emit("updateScript", this.currentRow);
      this.closeDialog();
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  },
};
</script>
